package com.example.springboot_jxc_0511.jxc.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springboot_jxc_0511.jxc.common.Constants;
import com.example.springboot_jxc_0511.jxc.entity.Users;
import com.example.springboot_jxc_0511.jxc.service.IUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gongxl
 * @since 2021-05-11
 */
@Controller
@RequestMapping("/users")
public class UsersController {
//    @RequestMapping("/{path}")
//    public String commonPath(@PathVariable(value = "path") String path) {
//        return path;
//    }
    @Autowired
    private IUsersService iUsersService;
    /**
     * @Author GongXl
     * @Description 跳转到登录页
     * @Date 2021/5/11 14:34
     * @Param [model]
     * @return java.lang.String
     **/
    @RequestMapping("/toLogin")
    public String toLogin(Model model) {
        model.addAttribute("users",new Users());
        return "login";
    }

    @RequestMapping("/login")
    public String login(Users users, Model model, HttpServletRequest req) {
        //查询用户名账号相等
        //账号密码相同，登录成功，否则重新登录
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username",users.getUsername());
        queryWrapper.eq("password",users.getPassword());
        Users userTemp = iUsersService.getOne(queryWrapper);
        if(null != userTemp){
            //登录成功
            req.getSession().setAttribute(Constants.USER_INFO,userTemp);
            //重定向到主页
            return "redirect:/sale/toMainXs";
        }else{
            model.addAttribute(Constants.ERROR_INFO,"账号或密码不正确");
            //转发
            return "login";
        }
    }
    /**
     * @Author GongXl
     * @Description 登出
     * @Date 2021/5/11 15:54
     * @Param [users, model, req]
     * @return java.lang.String
     **/
    @RequestMapping("/logout")
    public String login(HttpServletRequest req,Model model) {
        req.getSession().invalidate();
        model.addAttribute("users",new Users());
        return "login";
    }
}

